package xyz.penghaonan.p2plib;

import android.Manifest;
import android.app.Activity;
import android.app.Notification;
import android.content.pm.PackageManager;
import android.net.wifi.p2p.WifiP2pInfo;

import com.penghaonan.appframework.AppDelegate;
import com.penghaonan.appframework.utils.CommonUtils;

import xyz.penghaonan.p2plib.helper.connect.ClientConnectHelper;
import xyz.penghaonan.p2plib.helper.connect.ServerConnectHelper;
import xyz.penghaonan.p2plib.helper.message.IMsg;
import xyz.penghaonan.p2plib.helper.p2phelper.WifiP2PListenerAdapter;
import xyz.penghaonan.p2plib.helper.p2phelper.WifiP2PHelper;
import xyz.penghaonan.p2plib.helper.service.ServiceHelper;
import xyz.penghaonan.p2plib.helper.sockethelper.ClientSocketHelper;
import xyz.penghaonan.p2plib.helper.sockethelper.ServerSocketHelper;
import xyz.penghaonan.p2plib.utils.WifiP2PListenerLog;

public class P2PManager {

    private static ServiceHelper serviceHelper;
    private static WifiP2PHelper wifiP2PHelper;
    private static ClientSocketHelper clientSocketHelper;
    private static ServerSocketHelper serverSocketHelper;
    private static ClientConnectHelper clientConnectHelper;
    private static ServerConnectHelper serverConnectHelper;

    private static Config config;

    public static void setConfig(Config conf) {
        config = conf;
    }

    public static Config getConfig() {
        return config;
    }

    public static ServerConnectHelper getServerConnectHelper() {
        if (serverConnectHelper == null) {
            serverConnectHelper = new ServerConnectHelper();
        }
        return serverConnectHelper;
    }

    public static ClientConnectHelper getClientConnectHelper() {
        if (clientConnectHelper == null) {
            clientConnectHelper = new ClientConnectHelper();
        }
        return clientConnectHelper;
    }

    public static ServiceHelper getServiceHelper() {
        if (serviceHelper == null) {
            serviceHelper = new ServiceHelper();
        }
        return serviceHelper;
    }

    public static WifiP2PHelper getP2PHelper() {
        if (wifiP2PHelper == null) {
            wifiP2PHelper = new WifiP2PHelper();
            wifiP2PHelper.addListener(new WifiP2PListenerLog());
            wifiP2PHelper.addListener(new WifiP2PListenerAdapter() {

                @Override
                public void onConnectionInfoAvailable(WifiP2pInfo wifiP2pInfo) {
                    super.onConnectionInfoAvailable(wifiP2pInfo);
                    if (wifiP2pInfo == null) {
                        if (clientSocketHelper != null) {
                            getClientConnectHelper().onDisconnect();
                            clientSocketHelper.release();
                            clientSocketHelper = null;
                        }
                        if (serverSocketHelper != null) {
                            getServerConnectHelper().onDisconnect();
                            serverSocketHelper.release();
                            serverSocketHelper = null;
                        }
                    }
                }
            });
            wifiP2PHelper.registerReceiver();
        }
        return wifiP2PHelper;
    }

    public static ClientSocketHelper getClientSocketHelper() {
        if (clientSocketHelper == null) {
            clientSocketHelper = new ClientSocketHelper();
        }
        return clientSocketHelper;
    }

    public static ServerSocketHelper getServerSocketHelper() {
        if (serverSocketHelper == null || serverSocketHelper.isReleased()) {
            serverSocketHelper = new ServerSocketHelper();
        }
        return serverSocketHelper;
    }

    public static boolean hasAvailableServerSocket() {
        return serverSocketHelper != null && !serverSocketHelper.isReleased();
    }

    public static void requestPermission(Activity activity) {
        String[] permissionArray = {Manifest.permission.ACCESS_WIFI_STATE,
                Manifest.permission.CHANGE_WIFI_STATE,
                Manifest.permission.CHANGE_NETWORK_STATE,
                Manifest.permission.INTERNET,
                Manifest.permission.ACCESS_NETWORK_STATE,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.WRITE_EXTERNAL_STORAGE};

        CommonUtils.requestPermission(activity, permissionArray);
    }

    public static boolean isSupportWifiP2P() {
        return AppDelegate.getApp().getPackageManager() != null
                && AppDelegate.getApp().getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI_DIRECT);
    }

    public interface Config {
        Notification getServiceNotification();

        int getServiceNotificationId();

        void OnMsgReceived(IMsg msg);
    }
}
